package com.live.stamp.base.validation;

import com.hikeSoft.common.utils.BaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.container.core.common.BusinessException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 邮箱验证处理器
 */
public class EmailHandle extends ParamHandler {
    private static final Logger logger = LoggerFactory.getLogger(EmailHandle.class);

    /**
     * 邮箱格式
     */
    private static final Pattern PATTERN = Pattern.compile("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/");

    @Override
    protected ValidationEnum getParamHandleType() {
        return ValidationEnum.EMAIL;
    }

    @Override
    protected void echo(String paramName, Object param) {
        String email = (String)param;
        if(BaseUtils.isBlank(email)){
            logger.error("EmailHandle class -> param:{} of email is blank!", paramName);
            throw new BusinessException(String.format("邮箱%s为空", paramName));
        }

        Matcher m = PATTERN.matcher(email);
        if(!m.find()){
            logger.error("EmailHandle class -> format of email:{} is error!", email);
           throw new BusinessException(String.format("邮箱%s格式不正确", email));
        }
    }
}
